package edu.hubu.service.impl;

import edu.hubu.dao.impl.IStudentCourseDao;
import edu.hubu.dao.impl.ITeacherCourseDao;
import edu.hubu.model.Student;
import edu.hubu.model.StudentCourse;
import edu.hubu.model.TeacherCourse;
import net.moon.commons.ResultsJson;
import net.moon.commons.StatusCode;
import net.moon.jpa.dao.IBaseDao;
import net.moon.jpa.service.DefaultService;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author moonlan
 * date 2020/11/13 20:51
 */
@SuppressWarnings(value = "unused")
public class StudentService extends DefaultService<Student, String> {
    private final IStudentCourseDao studentCourseDao;

    private final ITeacherCourseDao teacherCourseDao;

    protected StudentService(IBaseDao<Student, String> dao, IStudentCourseDao studentCourseDao, ITeacherCourseDao teacherCourseDao) {
        super(dao, "studentIsForbidden", "");
        this.studentCourseDao = studentCourseDao;
        this.teacherCourseDao = teacherCourseDao;
    }

    public ResultsJson getCourses(String sid) {
        List<StudentCourse> all = studentCourseDao.findAll().stream().filter(studentCourse -> {
            List<Student> anElse = studentCourse.getStudents();
            Optional<Student> student = anElse.stream().filter(s -> !s.isUserIsDeleted() && !s.isUserIsForbidden() && s.getUserSid().equals(sid)).findFirst();
            return !studentCourse.isStudentCourseIsForbidden() && !studentCourse.isStudentCourseIsDeleted() && student.isPresent();
        }).collect(Collectors.toList());
        return ResultsJson.toJson(StatusCode.SUCCESS, all.size(), 0, all);
    }

    public ResultsJson delCourse(String id) {
        try {
            studentCourseDao.deleteById(id);
            return ResultsJson.toJson(StatusCode.SUCCESS, 1, 0, true);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public ResultsJson addCourses(String sid, TeacherCourse teacherCourse) {
        Student student = findEntityById(sid);
        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setCourse(teacherCourse.getCourse());
        studentCourse.getTeachers().addAll(teacherCourse.getTeachers());
        studentCourse.setStudentCourseStrategy(teacherCourse.getTeacherCourseStrategy());
        studentCourse.getStudentCourseTime().clear();
        studentCourse.getStudentCourseTime().addAll(teacherCourse.getTeacherCourseTime());
        studentCourse.setStudentCourseWeekDay(teacherCourse.getTeacherCourseWeekDay());
        StudentCourse save = studentCourseDao.save(studentCourse);
        student.getStudentCourses().add(save);
        Optional<TeacherCourse> byId = teacherCourseDao.findById(teacherCourse.getTeacherCourseId());
        if (byId.isPresent()) {
            TeacherCourse course = byId.get();
            course.setTeacherCourseStudentNums(course.getTeacherCourseStudentNums() + 1);
        }
        return ResultsJson.toJson(StatusCode.SUCCESS, 1, 0, updateEntityById(sid, student));
    }
}
